home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2006 December / PCWDEC06.iso / Software / Trial / Paint Shop Pro XI / Data1.cab / _7A8C70B42DC6496FB0B311DF2B7E358B < prev    next >
Encoding:
Text File  |  2006-08-04  |  10.2 KB  |  241 lines

  1. from PSPApp import *
  2. import PSPUtils
  3. import os.path
  4.  
  5. def ScriptProperties():
  6.     return {
  7.         'Author': u'Corel Corporation',
  8.         'Copyright': u'Copyright (c) 2002-2006 Corel Corporation. All rights reserved.',
  9.         'Description': "Make a pseudo-palette - a series of squares filled with solid colors",
  10.         'Host': u'Paint Shop Pro 9',
  11.         'Host Version': u'9.00',
  12.         }
  13.  
  14.  
  15. def Do(Environment):
  16.     if PSPUtils.RequireADoc( Environment ) == App.Constants.Boolean.false:
  17.         return
  18.     
  19.     # all of these formats have palettes, and we need a paletted image.
  20.     PalettedFormats = [ App.Constants.PixelFormat.Index1, App.Constants.PixelFormat.Index4,
  21.                         App.Constants.PixelFormat.Index8, App.Constants.PixelFormat.Grey,
  22.                         App.Constants.PixelFormat.GreyA ]
  23.     
  24.     # is the current image paletted?  If not, give them the option to reduce color depth
  25.     Info = App.Do( Environment, 'ReturnImageInfo', {'GeneralSettings': {'Version': ((9,0,0),1) }} )
  26.  
  27.     if not Info['PixelFormat'] in PalettedFormats:
  28.         result = App.Do(Environment,  'MsgBox', {
  29.                 'Buttons': App.Constants.MsgButtons.YesNo, 
  30.                 'Icon': App.Constants.MsgIcons.Question, 
  31.                 'Text': PSPUtils.RequiresPaletted,
  32.                 'GeneralSettings': {'Version': ((9,0,0),1) }
  33.                 })
  34.         if result == 0:
  35.             return
  36.         else:
  37.             # always do this interactively, even if the script as a whole is silent
  38.             App.Do( Environment, 'DecreaseColorsToX', {
  39.                         'GeneralSettings': {
  40.                             'ExecutionMode': App.Constants.ExecutionMode.Interactive, 
  41.                             'AutoActionMode': App.Constants.AutoActionMode.Match,
  42.                             'Version': ((9,0,0),1)
  43.                             }
  44.                         })
  45.  
  46.             # get a new copy of the image info since it just changed
  47.             Info = App.Do( Environment, 'ReturnImageInfo', {'GeneralSettings': {'Version': ((9,0,0),1) }} )
  48.  
  49.     # Save the palette we just generated
  50.     PalName, Ext = os.path.splitext(App.TargetDocument.Title)
  51.     App.Do( Environment, 'SavePalette', {
  52.             'Filename': PalName, 
  53.             'Format': App.Constants.PaletteFormat.PSPPaletteFormat, 
  54.             'Overwrite': App.Constants.Boolean.true, 
  55.             'GeneralSettings': {
  56.                 'ExecutionMode': App.Constants.ExecutionMode.Interactive,
  57.                 'AutoActionMode': App.Constants.AutoActionMode.Match,
  58.                 'Version': ((9,0,0),1)
  59.                 }
  60.             })
  61.             
  62.     ColorList = Info[ 'PaletteColorList' ]
  63.     if not ColorList:
  64.         print PSPUtils.NoPaletteFound
  65.         return
  66.  
  67.     # now make a pseudo-palette - an image with little swatches of color against a solid
  68.     # background.  Make the background a medium grey, and separate each swatch by 2 pixels
  69.     # of grout.  Each color square is 10x10
  70.     # *** Change these parameters for a different output image
  71.     MakePseudoPalette( Environment, ColorList, GroutColor=(128,128,128), GroutWidth=2,
  72.                        ColorsPerRow=16, TileSize=(10,10), ButtonMargin=0 )
  73.  
  74.     # we just created a new image - just leave it open for the user to decide what to do
  75.  
  76.  
  77.  
  78. # Create a new image that appears somewhat like a palette - a series of color swatches 
  79. # organized into rows and columns. The resulting image can be customized by setting the
  80. # parameters appropriately.
  81. # ColorList - list of (r,g,b) tuples with the colors to use.  Must be between 2 and 1024
  82. # GroutColor - underlying color of the image, seen between the color tiles.  Defaults to
  83. #              medium grey
  84. # GroutWidth - how much grout is seen between color tiles.  Defaults to 2 pixels.
  85. # ColorsPerRow - number of tiles on each row.  Along with TileSize[0], determines
  86. #                the width of the image. Defaults to 16
  87. # TileSize - (x,y) size tuple.  A rectangle of that size will be filled with each color.
  88. #            along with length of the colorlist and ColorsPerRow, determines the resulting
  89. #            size of the image.  Defaults to (10,10)
  90. # ButtonMargin - how much of a buttonize effect to apply to each color tile.  Default of 0
  91. #                means no buttonize is done.  A buttonize of 2 yields a slight 3D effect.
  92. def MakePseudoPalette( Environment, ColorList, GroutColor=(0,0,0),
  93.                        GroutWidth=2, ColorsPerRow=16, TileSize=(10,10), ButtonMargin=0):
  94.     # lets put some sanity checks on our arguments - these values are arbitrary for the
  95.     # most part, so if you don't like them, change them.
  96.     if not GroutWidth in range(0,20):
  97.         print PSPUtils.GroutWidthMsg
  98.         return 0
  99.     if not ColorsPerRow in range(1,100):
  100.         print PSPUtils.ColorsPerRowMsg
  101.         return 0
  102.     if not TileSize[0] in range(2, 50):
  103.         print PSPUtils.TileSizeMsg
  104.         return 0
  105.     if not TileSize[1] in range(2, 50):
  106.         print PSPUtils.TileSizeMsg
  107.         return 0
  108.     if not len(ColorList) in range( 2, 1024 ):
  109.         print PSPUtils.NumColorsMsg
  110.         return 0
  111.     if not ButtonMargin in range( 0, min(TileSize)/2 ):
  112.         print PSPUtils.ButtonMarginMsg
  113.         return 0
  114.     
  115.     # run through the list of colors we were given and remove consecutive duplicates
  116.     # this will address the issue of reducing to an odd number of colors, but permit
  117.     # duplicates to occur in the middle of the color list
  118.     OldColor = ( 1000, 0, 0 )   # not a legal color value, so should never occur in nature
  119.     iStartDup = 0
  120.     iCurrentIndex = 0
  121.     
  122.     for Color in ColorList:
  123.         if Color != OldColor:
  124.             iStartDup = iCurrentIndex
  125.             OldColor = Color
  126.             
  127.         iCurrentIndex += 1
  128.  
  129.     if iStartDup + 1 < len(ColorList):
  130.         ColorList = ColorList[ 0 : iStartDup+1 ]
  131.  
  132.     # start by determining how big a document we want to create
  133.     NumRows = (len(ColorList) + ColorsPerRow - 1) / ColorsPerRow
  134.     Height = (NumRows * TileSize[1]) + ((NumRows + 1) * GroutWidth)
  135.     Width = (ColorsPerRow * TileSize[0]) + ((ColorsPerRow + 1) * GroutWidth)
  136.  
  137.     # make an image of the proper size using a background of the grout color
  138.     App.Do( Environment, 'NewFile', {
  139.             'Width': Width,
  140.             'Height': Height, 
  141.             'ColorDepth': App.Constants.Colordepth.SixteenMillionColor, 
  142.             'DimensionUnits': App.Constants.DimensionType.Pixels, 
  143.             'FillMaterial': {
  144.                 'Color': GroutColor, 
  145.                 'Pattern': None, 
  146.                 'Gradient': None, 
  147.                 'Texture': None
  148.                 }, 
  149.             'Transparent': App.Constants.Boolean.false, 
  150.             'VectorBackground': App.Constants.Boolean.false,
  151.             'LayerType': 1,
  152.             'GeneralSettings': {
  153.                 'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  154.                 'AutoActionMode': App.Constants.AutoActionMode.Match,
  155.                 'Version': ((9,0,0),1)
  156.                 }
  157.             })
  158.  
  159.     TargetDoc = App.ActiveDocument
  160.     
  161.     # now iterate across the list of colors
  162.     RowNumber = 0
  163.     ColNumber = 0
  164.     RowStart = GroutWidth
  165.     ColStart = GroutWidth
  166.     Index = 0 
  167.     for Color in ColorList:
  168.         # just send data to the output so they know something is happening.
  169.         print PSPUtils.ColorIs % (Index, Color[0], Color[1], Color[2])
  170.         Index += 1
  171.         
  172.         # when we reach the end of a row reset our column and advance row
  173.         if ColNumber >= ColorsPerRow:
  174.             ColNumber = 0
  175.             RowNumber += 1
  176.             RowStart += TileSize[1] + GroutWidth
  177.             ColStart = GroutWidth
  178.  
  179.         # make a selection of the rectangle this color should cover
  180.         App.Do( Environment, 'Selection', {
  181.                 'General': {
  182.                     'Mode': App.Constants.SelectionOperation.Replace, 
  183.                     'Antialias': App.Constants.Boolean.false, 
  184.                     'Feather': 0
  185.                     }, 
  186.                 'SelectionShape': App.Constants.SelectionShape.Rectangle, 
  187.                 'Start': (ColStart, RowStart), 
  188.                 'End': (ColStart + TileSize[0], RowStart + TileSize[1]), 
  189.                 'GeneralSettings': {
  190.                     'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  191.                     'AutoActionMode': App.Constants.AutoActionMode.Match,
  192.                     'Version': ((9,0,0),1)
  193.                     }
  194.                 }, TargetDoc )
  195.  
  196.         # fill it with the color
  197.         App.Do( Environment, 'Fill', {
  198.                 'BlendMode': 0, 
  199.                 'MatchMode': 1, 
  200.                 'Material': {
  201.                     'Color': Color, 
  202.                     'Pattern': None, 
  203.                     'Gradient': None, 
  204.                     'Texture': None
  205.                     }, 
  206.                 'UseForground': App.Constants.Boolean.true, 
  207.                 'Opacity': 100, 
  208.                 'Point': (ColStart+1, RowStart+1), 
  209.                 'SampleMerged': 0, 
  210.                 'Tolerance': 20, 
  211.                 'GeneralSettings': {
  212.                     'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  213.                     'AutoActionMode': App.Constants.AutoActionMode.Match,
  214.                     'Version': ((9,0,0),1)
  215.                     }
  216.                 }, TargetDoc )
  217.  
  218.         # buttonize the color if requested
  219.         if ButtonMargin != 0:
  220.             App.Do( Environment, 'Buttonize', {
  221.                 'Width': ButtonMargin, 
  222.                 'Height': ButtonMargin, 
  223.                 'Transparent': App.Constants.Boolean.false, 
  224.                 'Color': (255,255,255), 
  225.                 'Opacity': 75, 
  226.                 'GeneralSettings': {
  227.                     'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  228.                     'AutoActionMode': App.Constants.AutoActionMode.Match,
  229.                     'Version': ((9,0,0),1)
  230.                     }
  231.                 }, TargetDoc)
  232.  
  233.         # go on to the next column        
  234.         ColNumber += 1
  235.         ColStart += TileSize[0] + GroutWidth
  236.         
  237.     # select none to get rid of the selection on the last color square.
  238.     App.Do( Environment, 'SelectNone', { 'GeneralSettings': {'Version': ((9,0,0),1) } }, TargetDoc )
  239.     
  240.     return 1 # success
  241.